package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.AuthDto;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.user.mapper.AuthMapper;
import com.heima.user.service.AuthService;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AuthServiceImpl extends ServiceImpl<AuthMapper, ApUserRealname> implements AuthService {
    //查询审核列表
    @Override
    public ResponseResult findList(AuthDto dto) {
        IPage pageInfo = new Page(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<ApUserRealname> lqw = new LambdaQueryWrapper<>();
        lqw.eq(dto.getStatus() != null, ApUserRealname::getStatus, dto.getStatus());
        lqw.orderByDesc(ApUserRealname::getUpdatedTime);

        pageInfo = page(pageInfo, lqw);
        ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) pageInfo.getTotal());
        responseResult.setData(pageInfo.getRecords());
        return responseResult;
    }

    //审核失败      2 审核失败  失败消息
    @Override
    public ResponseResult fail(AuthDto dto) {
        ApUserRealname user = getById(dto.getId());
        user.setReason(dto.getMsg());
        user.setStatus((short) 2);
        user.setUpdatedTime(new Date());
        updateById(user);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    //审核成功  9 审核成功
    @Override
    public ResponseResult success(AuthDto dto) {
        ApUserRealname user = getById(dto.getId());
        user.setStatus((short) 9);
        user.setUpdatedTime(new Date());
        updateById(user);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
